﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace LeagueUtils
{
    /*
     * This class dives into the new folder structure to find the needed files.
     * The purpose of each method should be self explanitory based on the method name...
     */
    public static class FileFinder
    {
        static String LoL_Location = LeagueUtils.Preferences.leagueFolder;
        static void Main(string[] args)
        {
            /*byte[] champData = File.ReadAllBytes("C:\\Games\\League of Legends\\Riot Games\\League of Legends\\RADS\\projects\\lol_game_client\\managedfiles\\0.0.0.15\\DATA\\Characters\\SadMummy\\SadMummy.inibin");
            IniBinParser inbin = new IniBinParser(champData);
            int[] itemData = inbin.GetRecommendedItems();*/
        }

        public static List<string> findFile(string fileToFind, string directory)
        {
            string b = directory;
            List<string> result = new List<string>();
            Stack<string> stack = new Stack<string>();
            List<string> finalResult = new List<string>();
            stack.Push(b);
            while (stack.Count > 0)
            {
                string dir = stack.Pop();
                try
                {
                    result.AddRange(Directory.GetFiles(dir, "*.*"));
                    foreach (string dn in Directory.GetDirectories(dir))
                    {
                        stack.Push(dn);
                    }
                }
                catch { }
            }
            foreach (string fileName in result)
            {
                if (fileName.ToLower().Contains(fileToFind.ToLower()))
                {
                    finalResult.Add(fileName);
                }
            }
            return finalResult;
        }

        public static string findChampionFolder(string championName)
        {
            try
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(LoL_Location + "\\RADS\\projects\\lol_game_client\\managedfiles");
                foreach (System.IO.DirectoryInfo subDirectory in dir.GetDirectories())
                {
                    if (new System.IO.FileInfo(subDirectory.FullName + "\\DATA\\Characters\\" + championName + "\\" + championName + "LoadScreen.DDS").Exists)
                        return subDirectory.FullName + "\\DATA\\Characters\\" + championName;
                }
                return null;
            }
            catch 
            {
                MessageBox.Show("Error:\nAn error occured while trying to find the " + championName + "LoadScreen.DDS file. This is most likely due to a misconfigured 'preferences.xml' file." +
                " Please correct this file or delete it, then relaunch the application", "Configuration Error");
                return null;
            }
        }

        public static string findChampImageFolder()
        {
            try
            {
                List<string> deployFolders = new List<string>();
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(LoL_Location + "\\RADS\\projects\\lol_air_client\\releases");
                foreach (System.IO.DirectoryInfo subDirectory in dir.GetDirectories())
                {
                    if (new System.IO.DirectoryInfo(subDirectory.FullName + "\\deploy\\assets\\images\\champions").Exists)
                        deployFolders.Add(subDirectory.FullName + "\\deploy\\assets\\images\\champions");
                }
                if (deployFolders.Count != 0)
                    return deployFolders[deployFolders.Count - 1];
                else
                    return null;
            }
            catch
            {
                MessageBox.Show("Error:\nAn error occured while trying to find the Champions folder. This is most likely due to a misconfigured 'preferences.xml' file." +
                " Please correct this file or delete it, then relaunch the application", "Configuration Error");
                return null;
            }
        }

        public static string findHUDFile(string fileName)
        {
            try
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(LoL_Location + "\\RADS\\solutions\\lol_game_client_sln\\releases");
                foreach (System.IO.DirectoryInfo subDirectory in dir.GetDirectories())
                {
                    if (new System.IO.FileInfo(subDirectory.FullName + "\\deploy\\DATA\\Menu\\hud\\" + fileName).Exists)
                        return (subDirectory.FullName + "\\deploy\\DATA\\Menu\\hud\\" + fileName);
                }
                return null;
            }
            catch
            {
                MessageBox.Show("Error:\nAn error occured while trying to find the HUD file. This is most likely due to a misconfigured 'preferences.xml' file." +
                " Please correct this file or delete it, then relaunch the application", "Configuration Error");
                return null;
            }
        }

        public static string findCfgFile()
        {
            try
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(LoL_Location + "\\RADS\\solutions\\lol_game_client_sln\\releases");
                foreach (System.IO.DirectoryInfo subDirectory in dir.GetDirectories())
                {
                    if (new System.IO.FileInfo(subDirectory.FullName + "\\deploy\\DATA\\CFG\\game.cfg").Exists)
                        return (subDirectory.FullName + "\\deploy\\DATA\\CFG\\game.cfg");
                }
                return null;
            }
            catch
            {
                MessageBox.Show("Error:\nAn error occured while trying to find the game.cfg file. This is most likely due to a misconfigured 'preferences.xml' file."+
                " Please correct this file or delete it, then relaunch the application" , "Configuration Error");
                return null;
            }
        }

        public static string findSummonerSpells()
        {
            try
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(LoL_Location + "\\RADS\\projects\\lol_air_client\\releases\\");
                foreach (System.IO.DirectoryInfo subDirectory in dir.GetDirectories())
                {
                    if (new System.IO.FileInfo(subDirectory.FullName + "\\deploy\\assets\\images\\spells\\Spell_SummonerBoost.png").Exists)
                        return (subDirectory.FullName + "\\deploy\\assets\\images\\spells");
                }
                return null;
            }
            catch
            {
                MessageBox.Show("Error:\nAn error occured while trying to find the Summoner Spells folder. This is most likely due to a misconfigured 'preferences.xml' file." +
                " Please correct this file or delete it, then relaunch the application", "Configuration Error");
                return null;
            }
        }

        public static string fontConfigFinder()
        {

            List<string> menuFolders = findFile("fontConfig_", LoL_Location + "\\RADS\\projects\\");
            long fileSize = 0;
            String fileName = "";
            if (menuFolders.Count != 0)
            {
                for (int i = 0; i < menuFolders.Count; i++)
                {
                    FileInfo fi = new FileInfo(menuFolders[i]);
                    if (fi.Length > fileSize)
                    {
                        fileSize = fi.Length;
                        fileName = menuFolders[i];
                    }
                }
                return fileName;
            }
            else
            {
                MessageBox.Show("Error:\nAn error occured while trying to find the fontConfig file. This is most likely due to a misconfigured 'preferences.xml' file." +
                " Please correct this file or delete it, then relaunch the application", "Configuration Error");
                return null;
            }
            /*try
            {
                List<string> menuFolders = new List<string>();
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(LoL_Location + "\\RADS\\projects\\lol_game_client_en_us\\managedfiles");
                foreach (System.IO.DirectoryInfo subDirectory in dir.GetDirectories())
                {
                    if (new System.IO.FileInfo(subDirectory.FullName + "\\DATA\\Menu\\fontConfig_en_US.txt").Exists)
                        menuFolders.Add(subDirectory.FullName + "\\DATA\\Menu\\fontConfig_en_US.txt");
                }
                if (menuFolders.Count != 0)
                    return menuFolders[menuFolders.Count - 1];
                else
                {
                    dir = new System.IO.DirectoryInfo(LoL_Location + "\\RADS\\projects\\lol_game_client_en_gb\\managedfiles");
                    foreach (System.IO.DirectoryInfo subDirectory in dir.GetDirectories())
                    {
                        if (new System.IO.FileInfo(subDirectory.FullName + "\\DATA\\Menu\\fontConfig_en_GB.txt").Exists)
                            menuFolders.Add(subDirectory.FullName + "\\DATA\\Menu\\fontConfig_en_GB.txt");
                    }
                    if (menuFolders.Count != 0)
                        return menuFolders[menuFolders.Count - 1];
                    else
                        return null;
                }
            }
            catch
            {
                MessageBox.Show("Error:\nAn error occured while trying to find the fontConfig file. This is most likely due to a misconfigured 'preferences.xml' file." +
                " Please correct this file or delete it, then relaunch the application", "Configuration Error");
                return null;
            }*/
        }

        public static string findChampionMP3()
        {
            try
            {
                List<string> deployFolders = new List<string>();
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(LoL_Location + "\\RADS\\projects\\lol_air_client\\releases");
                foreach (System.IO.DirectoryInfo subDirectory in dir.GetDirectories())
                {
                    if (new System.IO.DirectoryInfo(subDirectory.FullName + "\\deploy\\assets\\sounds\\en_US\\champions").Exists)
                        deployFolders.Add(subDirectory.FullName + "\\deploy\\assets\\sounds\\en_US\\champions");
                }
                if (deployFolders.Count != 0)
                    return deployFolders[deployFolders.Count - 1];
                else
                    return null;
            }
            catch
            {
                MessageBox.Show("Error:\nAn error occured while trying to find the champion mp3 folder. This is most likely due to a misconfigured 'preferences.xml' file." +
                " Please correct this file or delete it, then relaunch the application", "Configuration Error");
                return null;
            }
        }

        public static string findItemIcons()
        {
            //C:\Games\League of Legends\Riot Games\League of Legends\RADS\projects\lol_air_client\releases\0.0.0.13\deploy\assets\images\items
            try
            {
                List<string> deployFolders = new List<string>();
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(LoL_Location + "\\RADS\\projects\\lol_air_client\\releases");
                foreach (System.IO.DirectoryInfo subDirectory in dir.GetDirectories())
                {
                    if (new System.IO.DirectoryInfo(subDirectory.FullName + "\\deploy\\assets\\images\\items").Exists)
                        deployFolders.Add(subDirectory.FullName + "\\deploy\\assets\\images\\items");
                }
                if (deployFolders.Count != 0)
                    return deployFolders[deployFolders.Count - 1];
                else
                    return null;
            }
            catch
            {
                MessageBox.Show("Error:\nAn error occured while trying to find the item image folder. This is most likely due to a misconfigured 'preferences.xml' file." +
                " Please correct this file or delete it, then relaunch the application", "Configuration Error");
                File.Delete("preferences.xml");
                return null;
            }
        }

        public static List<string> findInibinFiles()
        {
            string b = Preferences.leagueFolder;
            List<string> result = new List<string>();
            Stack<string> stack = new Stack<string>();
            List<string> finalResult = new List<string>();
            stack.Push(b);
            while (stack.Count > 0)
            {
                string dir = stack.Pop();
                try
                {
                    result.AddRange(Directory.GetFiles(dir, "*.*"));
                    foreach (string dn in Directory.GetDirectories(dir))
                    {
                        stack.Push(dn);
                    }
                }
                catch { }
            }
            foreach (string fileName in result)
            {
                if (fileName.Contains(".inibin"))
                {
                    finalResult.Add(fileName);
                }
            }
            return finalResult;
        }
    }
}
